package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-10-09
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService SubjectService) {
    try {
        //获取文件上传的流
        InputStream in = file.getInputStream();
        //真正的调用方法读取
        EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(SubjectService)).sheet().doRead();
    }catch (Exception e){
        e.printStackTrace();
    }
    }
    //课程分类列表（树形）
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);
        //查询二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> TwoSubjectList = baseMapper.selectList(wrapperTwo);
        //创建集合，封装最后的数据
        ArrayList<OneSubject> finalSubjectList = new ArrayList<>();
        //封装一级分类
        for (int i=0;i<oneSubjectList.size();i++){
            //获取一级分类的对象数据
            EduSubject eduSubject = oneSubjectList.get(i);
            //创建新的对象
            OneSubject oneSubject = new OneSubject();
            //将一个对象的值赋予另外一个对象
            BeanUtils.copyProperties(eduSubject,oneSubject);
            //将对象加入集合之中
            finalSubjectList.add(oneSubject);

            //封装二级分类
        //创建保护二级分类的集合
        ArrayList<TwoSubject> twoFinalSubjectList = new ArrayList<>();
        for (int m=0;m<TwoSubjectList.size();m++) {

            //获取每一个二级对象
            EduSubject tSubject = TwoSubjectList.get(m);
            //如果二级id与一级一样，那就是一级的子类
            if (tSubject.getParentId().equals(eduSubject.getId())) {
                TwoSubject twoSubject = new TwoSubject();
                BeanUtils.copyProperties(tSubject, twoSubject);
                twoFinalSubjectList.add(twoSubject);
            }
        }
        //把一级分类下面所有的二级分类放到一级分类去
            oneSubject.setChildren(twoFinalSubjectList);
        }
        return finalSubjectList;
    }
}
